/**
 * Copyright (c) 2013-Now http://jeesite.com All rights reserved.
 */
package com.cncqs.backend.employee.web;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.cncqs.backend.store.entity.ComStore;
import com.cncqs.backend.store.service.ComStoreService;
import com.jeesite.common.collect.ListUtils;
import com.jeesite.common.collect.MapUtils;
import com.jeesite.common.lang.StringUtils;
import com.jeesite.modules.sys.entity.Office;
import com.jeesite.modules.sys.utils.EmpUtils;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.jeesite.common.config.Global;
import com.jeesite.common.entity.Page;
import com.jeesite.common.web.BaseController;
import com.cncqs.backend.employee.entity.ComEmployee;
import com.cncqs.backend.employee.service.ComEmployeeService;

import java.util.List;
import java.util.Map;

/**
 * com_employeeController
 * @author cxy
 * @version 2019-04-23
 */
@Controller
@RequestMapping(value = "${adminPath}/employee/comEmployee")
public class ComEmployeeController extends BaseController {

	@Autowired
	private ComEmployeeService comEmployeeService;
	@Autowired
	private ComStoreService comStoreService;


	/**
	 * 获取数据
	 */
	@ModelAttribute
	public ComEmployee get(Long id, boolean isNewRecord) {
		return comEmployeeService.get(id == null ? "" : id.toString(), isNewRecord);
	}
	
	/**
	 * 查询列表
	 */
	@RequiresPermissions("employee:comEmployee:view")
	@RequestMapping(value = {"list", ""})
	public String list(ComEmployee comEmployee, Model model) {
		model.addAttribute("comEmployee", comEmployee);
		return "backend/employee/comEmployeeList";
	}
	
	/**
	 * 查询列表数据
	 */
	@RequiresPermissions("employee:comEmployee:view")
	@RequestMapping(value = "listData")
	@ResponseBody
	public Page<ComEmployee> listData(ComEmployee comEmployee, HttpServletRequest request, HttpServletResponse response) {
		if (org.apache.commons.lang3.StringUtils.isBlank(comEmployee.getEmployee().getOffice().toString()) && EmpUtils.getOffice() != null) {
			Office office = new Office();
			office.setOfficeCode(EmpUtils.getOffice().getOfficeCode());
			comEmployee.getEmployee().setOffice(office);
		}
		comEmployee.getEmployee().getOffice().setIsQueryChildren(Boolean.valueOf(true));
		comEmployee.getEmployee().getCompany().setIsQueryChildren(Boolean.valueOf(true));
		comEmployee.setPage(new Page<>(request, response));
		Page<ComEmployee> page = comEmployeeService.findPage(comEmployee);
		return page;
	}

	/**
	 * 查看编辑表单
	 */
	@RequiresPermissions("employee:comEmployee:view")
	@RequestMapping(value = "form")
	public String form(ComEmployee comEmployee, Model model) {
		model.addAttribute("comEmployee", comEmployee);
		return "backend/employee/comEmployeeForm";
	}

	/**
	 * 保存com_employee
	 */
	@RequiresPermissions("employee:comEmployee:edit")
	@PostMapping(value = "save")
	@ResponseBody
	public String save(@Validated ComEmployee comEmployee) {
		comEmployeeService.save(comEmployee);
		return renderResult(Global.TRUE, text("保存com_employee成功！"));
	}
	
	/**
	 * 停用com_employee
	 */
	@RequiresPermissions("employee:comEmployee:edit")
	@RequestMapping(value = "disable")
	@ResponseBody
	public String disable(ComEmployee comEmployee) {
		comEmployee.setStatus(ComEmployee.STATUS_DISABLE);
		comEmployeeService.updateStatus(comEmployee);
		return renderResult(Global.TRUE, text("停用com_employee成功"));
	}
	
	/**
	 * 启用com_employee
	 */
	@RequiresPermissions("employee:comEmployee:edit")
	@RequestMapping(value = "enable")
	@ResponseBody
	public String enable(ComEmployee comEmployee) {
		comEmployee.setStatus(ComEmployee.STATUS_NORMAL);
		comEmployeeService.updateStatus(comEmployee);
		return renderResult(Global.TRUE, text("启用com_employee成功"));
	}
	
	/**
	 * 删除com_employee
	 */
	@RequiresPermissions("employee:comEmployee:edit")
	@RequestMapping(value = "delete")
	@ResponseBody
	public String delete(ComEmployee comEmployee) {
		comEmployeeService.delete(comEmployee);
		return renderResult(Global.TRUE, text("删除com_employee成功！"));
	}

	/**
	 * 获取树结构数据
	 * @param excludeCode 排除的Code
	 * @param isShowCode 是否显示编码（true or 1：显示在左侧；2：显示在右侧；false or null：不显示）
	 * @return
	 */
	@RequiresPermissions("employee:comEmployee:view")
	@RequestMapping(value = "treeData")
	@ResponseBody
	public List<Map<String, Object>> treeData(String excludeCode, String isShowCode) {
		List<Map<String, Object>> mapList = ListUtils.newArrayList();

		List<ComStore> list = comStoreService.findList(new ComStore());
		for (int i=0; i<list.size(); i++){
			ComStore e = list.get(i);
			// 过滤非正常的数据
//			if (!TestTree.STATUS_NORMAL.equals(e.getStatus())){
//				continue;
//			}
			// 过滤被排除的编码（包括所有子级）
			if (StringUtils.isNotBlank(excludeCode)){
				if (e.getId().equals(excludeCode)){
					continue;
				}

			}
			Map<String, Object> map = MapUtils.newHashMap();
			map.put("id", e.getId());

			map.put("name", StringUtils.getTreeNodeName(isShowCode, e.getId().toString(), e.getName()));
			mapList.add(map);
		}
		return mapList;
	}

	@RequiresPermissions({"employee:comEmployee:view"})
	@RequestMapping({"index"})
	public String index(ComEmployee comEmployee, Model model) {
		return "backend/employee/comEmployeeIndex";
	}
	
}